package com.jike.jkgame;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jike.jkgame.data.Status;
import com.jike.jkgame.data.UserStatus;

public class UserStatusViewManager implements OnClickListener {

	public static final int FLAG_ALL = 0xFFFFFFFF;
	public static final int EXPERIENCE = 1;
	public static final int ENERGY = 1 << 1;;
	public static final int VIGOUR = 1 << 2;
	public static final int MONEY = 1 << 3;
	public static final int CASH = 1 << 4;
	public static final int LEVEL = 1 << 5;

	public static final int STATIC = 1 << 31;
	
	private static UserStatusViewManager mInstance;

	private boolean mClickable = true;
	private ViewHolder mViewHolder;
	
	private UserStatusViewManager()  {
	}
	
	public static UserStatusViewManager get() {
		if (mInstance == null) {
			mInstance = new UserStatusViewManager();
		}
		return mInstance;
	}

	public void setClickable(boolean clickable) {
		mClickable = clickable;
	}

	@Override
	public void onClick(View v) {
		if (mClickable) {
			 Intent intent = new
			 Intent(v.getContext(), UserInfoActivity.class);
			 JKApp.get().startActivity(intent);
		}
	}

	public void bindView(View parent) {
		mClickable = parent.isClickable();
		mViewHolder = (ViewHolder) parent.getTag();
		if (mViewHolder == null) {
			mViewHolder = new ViewHolder();

			Typeface font = Typeface.createFromAsset(parent.getContext()
					.getAssets(), "font.ttf");

			mViewHolder.nameTv = (TextView) parent
					.findViewById(R.id.tv_username);
			mViewHolder.nameTv.setTypeface(font);

			mViewHolder.levelTv = (TextView) parent.findViewById(R.id.tv_level);
			mViewHolder.levelTv.setTypeface(font);
			mViewHolder.experiencePb = (ProgressBar) parent
					.findViewById(R.id.pb_level);

			mViewHolder.energyPb = (ProgressBar) parent
					.findViewById(R.id.pb_energy);
			mViewHolder.energyTimerTv = (TextView) parent
					.findViewById(R.id.tv_energy_timer);

			mViewHolder.vigourPb = (ProgressBar) parent
					.findViewById(R.id.pb_vigour);
			mViewHolder.vigourTimerTv = (TextView) parent
					.findViewById(R.id.tv_vigour_timer);

			mViewHolder.coinsTv = (TextView) parent.findViewById(R.id.tv_coins);
			mViewHolder.goldsTv = (TextView) parent.findViewById(R.id.tv_golds);
			
			parent.setTag(mViewHolder);
		}
		updateView(FLAG_ALL);
	}
	
	public void unbind() {
		mViewHolder = null;
	}

	public void updateEnergyTimer(int seconds) {
		if (mViewHolder != null) {
			mViewHolder.energyTimerTv.setText(Utilis.fromatTime(seconds));
		}
	}

	public void updateVigourTimer(int seconds) {
		if (mViewHolder != null) {
			mViewHolder.vigourTimerTv.setText(Utilis.fromatTime(seconds));
		}
	}

	static class ViewHolder {
		public TextView nameTv;
		public TextView levelTv;
		public ProgressBar experiencePb;
		// public TextView experienceTv;
		public ProgressBar energyPb;
		// public TextView energyTv;
		public TextView energyTimerTv;
		public ProgressBar vigourPb;
		// public TextView strengthTv;
		public TextView vigourTimerTv;

		public TextView coinsTv;
		public TextView goldsTv;
	}

	public void updateView(int flag) {
		if (flag == 0 || mViewHolder == null) {
			return;
		}
		UserStatus user = UserStatus.get();

		if ((flag & STATIC) != 0) {
			mViewHolder.nameTv.setText(user.getName());
		}

		if ((flag & LEVEL) != 0) {
			mViewHolder.levelTv.setText(String.valueOf(user.getLevel()));
		}

		if ((flag & EXPERIENCE) != 0) {
			// 经验
			mViewHolder.experiencePb.setMax(user.getFullExperience());
			mViewHolder.experiencePb.setProgress(user.getExperience());
			// mViewHolder.experienceTv.setText(String.format("%d/%d",
			// user.getExperience(), user.getFullExperience()));
		}

		if ((flag & ENERGY) != 0) {
			// 能量
			Status status = user.getEnergy();
			mViewHolder.energyPb.setMax(status.maxValue);
			mViewHolder.energyPb.setProgress(status.value);
			// mViewHolder.energyTv.setText(String.format("%d/%d", status.value,
			// status.maxValue));
		}

		if ((flag & VIGOUR) != 0) {
			// 精力
			Status status = user.getVigour();
			mViewHolder.vigourPb.setMax(status.maxValue);
			mViewHolder.vigourPb.setProgress(status.value);
			// mViewHolder.strengthTv.setText(String.format("%d/%d",
			// status.value,
			// status.maxValue));

		}

		if ((flag & MONEY) != 0) {
			mViewHolder.coinsTv.setText(String.valueOf(user.getMoney()));
		}

		if ((flag & CASH) != 0) {
			mViewHolder.goldsTv.setText(String.valueOf(user.getCash()));
		}
	}

}
